package com.zerdoor.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/9/10.
 */
public class AnalyzeTabel {

    public static void main(String[] args) {
        try {
            writeReportJSPFile("E:\\123.txt", "hello");
//            System.out.println(getReportJSPTitle("sdsdsdsd"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建新导出报表的JSP文件
     * @param path 路径和文件名
     * @param content 文件主体内容部分
     */
    public static void writeReportJSPFile(String path, String content) throws Exception{

            File f = new File(path);
            //默认创建新文件
            f.createNewFile();

            String jsp = getReportJSPContent(content);

            BufferedWriter output = new BufferedWriter(new FileWriter(f));
            output.write(jsp);
            output.close();

    }

    /**
     * 传入html body 获取报表JSP内容字符串
     * @return
     */
    private static String getReportJSPContent(String htmlBody){

        String javaCode = "<%\n" +
                "\t\tString exportToExcel = request.getParameter(\"exportToExcel\");\n" +
                "\t\tif (exportToExcel != null\n" +
                "\t\t\t\t&& exportToExcel.toString().equalsIgnoreCase(\"YES\")) {\n" +
                "\t\t\tresponse.setContentType(\"application/vnd.ms-excel\");\n" +
                "\t\t\tresponse.setHeader(\"Content-Disposition\", \"inline; filename=\"\n" +
                "\t\t\t\t\t+ request.getParameter(\"excelName\") +\".xls\");\n" +
                "\n" +
                "\t\t}\n" +
                "\t%>";


        String jspHtml = "<%@ page language=\"java\" contentType=\"text/html; charset=gb2312\"\n" +
                "\tpageEncoding=\"gb2312\"%>\n" +
                "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
                "<html>\n" +

                "<head>\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n" +
                "<title>Export to Excel - Demo</title>\n" +

                "<style>\n" +
                "\t\ttable {\n" +
                "\t\t\tborder-collapse:collapse; \n" +
                "\t\t}\n" +
                "\t\tth,td{\n" +
                "\t\t\tborder:1px solid #C0C0C0;\n" +
                "\t\t\t\n" +
                "\t\t}\n" +
                "\t</style>" +

                "</head>" +
                "<body>" +
                javaCode + "\n" +
                htmlBody + "\n" +
                "</body>\n" +
                "</html>";




        return jspHtml;
    }


}
